package beusoft.ccjk.com.datastructures.stackAndQueue3.sevenImplementationofLoopQueue.src;

public class LoopQueue2<E> implements Queue<E> {

    private E[] data;
    private int front = 0, tail = 0;
    // LoopQueue中不声明size，如何完成所有的逻辑？
    // 这个问题可能会比大家想象的要难一点点：）

    //1. front<=tail<=data.length-1
    // 2  tail<front

    public LoopQueue2(int capacity) {
        data = (E[]) new Object[capacity + 1];
        front = 0;
        tail = 0;
    }

    public LoopQueue2() {
        this(10);
    }

    public int getCapacity() {
        return data.length - 1;
    }

    @Override
    public boolean isEmpty() {
        return getSize() == 0;
    }

    @Override
    public int getSize() {
        if (front <= tail) {
            return tail - front;
        } else {
            // 0 , 1 ,2, 3, 4 ,5 ,6, 7
            //front=6,tail=2 ,length=8
            // 2+2=4
            return data.length - front + tail;
        }
    }

    @Override
    public void enqueue(E e) {
        int size = getSize();
        int capacity = getCapacity();
        if (size == capacity)
            resize(getCapacity() * 2);

        data[tail] = e;
        tail = (tail + 1) % data.length;
        size = size + 1;
    }

    @Override
    public E dequeue() {

        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");

        E ret = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        if (getSize() == getCapacity() / 4 && getCapacity() / 2 != 0)
            resize(getCapacity() / 2);
        return ret;
    }

    @Override
    public E getFront() {
        if (isEmpty())
            throw new IllegalArgumentException("Queue is empty.");
        return data[front];
    }

    private void resize(int newCapacity) {

//        int oldCapacity = getCapacity();
        E[] newData = (E[]) new Object[newCapacity + 1];
        int size = getSize();
        for (int i = 0; i < size; i++)
            newData[i] = data[(i + front) % data.length];
        data = newData;
        front = 0;
        tail = size;
//        //区分是一下是扩容还是减容, 主要是要计算对tail的值
//        if (newCapacity < oldCapacity) {
//
//        } else {
//            tail = size;
//        }


    }

    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();
        res.append(String.format("Queue: size = %d , capacity = %d\n", getSize(), getCapacity()));
        res.append("front [");
        for (int i = front; i != tail; i = (i + 1) % data.length) {
            res.append(data[i]);
            if ((i + 1) % data.length != tail)
                res.append(", ");
        }
        res.append("] tail; ");
        res.append("front=").append(front);
        res.append("tail=").append(tail);
        return res.toString();
    }

    public static void main(String[] args) {

        LoopQueue2<Integer> queue = new LoopQueue2<>();
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);

            if (i % 3 == 2) {
                queue.dequeue();
                System.out.println(queue);
            }
        }

        while (!queue.isEmpty()) {
            System.out.println(queue.dequeue());
        }
    }
}
